{extend name="public/world"}
{block name="style"}{/block}
{block name="container"}
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form" lay-filter="formSearch">
                <div class="layui-field-box" style="padding:0;">
                    <div class="layui-form-item" style="margin-bottom:0">
                        <div class="layui-inline my-search">
                            <div class="layui-input-inline" style="width: 120px;">
                                <input type="text" name="start_at" class="layui-input" id="laydate-start" placeholder="会计期间" lay-key="1" value="">
                            </div>
                            <div class="layui-input-inline" style="width: 120px;">
                                <select name="status" lay-filter="status">
                                    <option value="">全部</option>
                                    <option value="0">未审核</option>
                                    <option value="1">已审核</option>
                                </select>
                            </div>
                            <div class="layui-input-inline">
                                <input type="text" name="period_code" value="" placeholder="按凭证号" autocomplete="off" class="layui-input" lay-filter="period_code">
                            </div>
                            <div class="layui-input-inline">
                                <input type="text" name="description" value="" placeholder="按摘要" autocomplete="off" class="layui-input" lay-filter="description">
                            </div>
                            <div class="layui-input-inline" style="width: 170px;">
                                <button type="button" class="layui-btn layui-btn-sm layui-btn-radius layui-btn-primary" title="清除条件" style="padding: 0 8px;" lay-submit lay-filter="reset"><i class="layui-icon" style="color:#009688;">&#xe640;</i></button>
                                <button type="button" class="layui-btn layui-btn-sm layui-btn-radius" lay-submit lay-filter="search">搜索</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <table class="layui-hide" id="dataTable" lay-filter="dataTable"></table>
        </div>
    </div>
</div>
{/block}
{block name="script"}
<script type="text/html" id="barDemo">
    {if isset($sonAuth['edit'])}
    {{# if(d.status == 0 && d.luck == 0) { }}
    <a href="{:url('/erp.FinanceVoucher/edit')}?period_code={{ d.period_code }}" class="layui-btn layui-btn-normal layui-btn-xs j-iframe-pop" lxb-data="{width: '95%', height: '95%'}" title="修改客户">修改</a>
    {{# } }}
    {/if}
    {if isset($sonAuth['del'])}
    {{# if(d.status == 0 && d.luck == 0) { }}
    <a href="{:url('/erp.FinanceVoucher/del')}?period_code={{ d.period_code }}" class="layui-btn layui-btn-danger layui-btn-xs j-tr-del">删除</a>
    {{# } }}
    {/if}
</script>
<script type="text/html" id="toolbar">
    <div class="layui-btn-groupB">
        {if isset($sonAuth['add'])}
        <a href="{:url('/erp.FinanceVoucher/add')}" class="layui-btn layui-btn-sm layuiadmin-btn-admin j-iframe-pop" lxb-data="{width: '95%', height: '95%'}" title="添加凭证">添加凭证</a>
        {/if}
    </div>
</script>
<script>
    layui.use(['form','table','tableMerge','laydate'], function() {
        var $ = layui.jquery
            , table = layui.table
            , form = layui.form
            , laydate = layui.laydate
            , tableMerge = layui.tableMerge;
        //渲染
        laydate.render({
            elem: '#laydate-start'
            ,type: 'month'
            //,max:'nowTime'
            ,min:'2021-01-01'
            ,value: new Date()
            ,ready: function(date){
                $('#layui-laydate1').on('click','ul.laydate-month-list li', function() {
                    $(this).hasClass('.laydate-disabled') || $('#layui-laydate1').find('.laydate-btns-confirm').click();
                });
            }
            ,done: function(value, date){
                setTimeout(function(){
                    $('#seachSub').click();
                }, 100);
            }
        });
        form.on('submit(search)', function(){
            tableIns.reload({
                where: form.val("formSearch"),
                page: {curr: 1}
            });
        });
        //头工具栏事件
        form.on('submit(reset)', function(){
            var searchs = form.val("formSearch");
            for (var i in searchs) {
                if(i != 'start_at') searchs[i] = '';
            }
            form.val("formSearch", searchs);
            tableIns.reload({
                where: form.val("formSearch"),
                page: {curr: 1}
            });
        });
        var tableIns = table.render({
            elem: '#dataTable'
            ,url: "{:url('/erp.FinanceVoucher/index')}" //数据接口
            ,where: form.val("formSearch")
            ,page: false //开启分页
            //,skin: 'line'
            ,loading:true
            ,even: false
            ,size: 'sm'
            ,limit: 10
            ,text: {
                none : '暂无凭证'
            }
            ,toolbar: '#toolbar'
            ,defaultToolbar: false
            ,cols: [[ //表头
                {field: 'period',merge: ['period_code', 'period'], width: 70, title: '会计期间'}
                ,{field: 'description',merge: ['period_code', 'description'], width: 160, title: '摘要'}
                ,{field: 'period_code',merge: true, width: 90, title: '凭证编号'}
                ,{field: 'code_name', width: 300, title: '科目'}
                ,{field: 'debit', width: 80, title: '借方金额'}
                ,{field: 'credit', width: 80, title: '贷方金额'}
                ,{field: 'period_at',merge: ['period_code', 'period_at'], width: 85, title: '凭证日期'}
                ,{field: 'period_code',merge: true, width: 110, title: '操作', templet: '#barDemo'}
                ,{field: 'luck',merge: ['period_code', 'luck'], width: 70, title: '结帐状态',templet:function(d){
                    if(d.luck == 1) return '<div class="cok">已锁帐</div>';
                    else return '<div>未锁帐</div>';
                }}
                ,{field: 'status',merge: ['period_code', 'status'], width: 70, title: '审核状态',templet:function(d){
                    if(d.status == 1) return '<div class="cok">已审核</div>';
                    else return '<div>未审核</div>';
                }}
                ,{field: 'create_user',merge: ['period_code', 'create_user'], width: 70, title: '制表人'}
                ,{field: 'check_user',merge: ['period_code', 'check_user'], width: 70, title: '复核人'}
                ,{field: 'order_no',merge: ['period_code', 'order_no'], width: 140, title: '原始单号'}
                ,{field: 'market',merge: ['period_code', 'market'], width: 120, title: '备注'}
            ]]
            ,done: function(res, curr, count){
                tableMerge.render(this);
                if(res.luck_btn) {
                    var btn = res.luck==0 ? '<a href="{:url(\'/erp.FinanceVoucher/luck\')}?period='+res.period+'" class="layui-btn layui-btn-danger layui-btn-sm b-ajax" confirm="确定要对期间【'+res.period+'】进行锁帐吗？">期间'+res.period+' 锁帐</a> <a href="{:url(\'/erp.FinanceVoucher/position\')}?period='+res.period+'" class="layui-btn layui-btn-primary layui-btn-sm b-ajax" confirm="确定要对期间【'+res.period+'】进行重新排序吗？">期间'+res.period+' 重新排序</a>' : '<a href="{:url(\'/erp.FinanceVoucher/luck\')}?period='+res.period+'" class="layui-btn layui-btn-warm layui-btn-sm b-ajax" confirm="确定要解除对期间【'+res.period+'】的锁帐吗？">期间'+res.period+' 解除锁帐</a>';
                } else {
                    var btn = '<a href="{:url(\'/erp.FinanceVoucher/position\')}?period='+res.period+'" class="layui-btn layui-btn-primary layui-btn-sm b-ajax" confirm="确定要对期间【'+res.period+'】进行重新排序吗？">期间'+res.period+' 重新排序</a>';
                }
                $('.layui-btn-groupB').find('.b-ajax').remove();
                $('.layui-btn-groupB').append(btn);
                $('.b-ajax').click(function(){
                    var that = $(this),
                        href = that.attr('href');
                    if (!href) {
                        layer.msg('请设置href参数');
                        return false;
                    }
                    layer.confirm(that.attr('confirm'), {title:false, closeBtn:0}, function(){
                        layer.msg('提交中...', {time:500000});
                        $.post(href, {}, function(res) {
                            layer.msg(res.msg);
                            if (res.code == 1) {
                                tableIns.reload();
                            }
                            setTimeout(function(){
                                layer.closeAll();
                            }, 1500);
                        });
                    });
                    return false;
                });
            }
        });
        table.on('row(dataTable)', function(obj){
            if($(obj.tr).hasClass('row-in')) {
                //$(obj.tr).removeClass('row-in');
            } else {
                $(obj.tr).addClass('row-in').siblings('tr').removeClass('row-in');
            }
        });
        table.on('rowDouble(dataTable)', function(obj){
            $(obj.tr).addClass('row-in').siblings('tr').removeClass('row-in');
            var period_code = obj.data.period_code;
            var them = window.top !== window.self ? window.top : window.self;
            them.layer.open({
                type: 2
                ,title: '查看凭证'
                ,content: "{:url('/erp.FinanceVoucher/show')}?period_code="+period_code
                ,maxmin: true
                ,area: ['95%', '95%']
                ,btn: ['关闭']
                ,btnAlign: 'c' //按钮居中
                ,fixed: false //不固定
                ,moveOut: true //true  可以拖出窗外  false 只能在窗内拖
                ,anim: 5 //出场动画 isOutAnim bool 关闭动画
                ,offset: 'auto' //['100px','100px'],//'auto',//初始位置  ['100px','100px'] t[ 上 左]
                ,shade: .7 //遮罩
                ,resize: true //是否允许拉伸
                ,move: '.layui-layer-title' // 默认".layui-layer-title",// 触发拖动的元素

                ,success: function(layero, index){
                    layero.find('.layui-layer-min').remove();
                    layero.find('.layui-layer-close2').addClass('layui-layer-close1').removeClass('layui-layer-close2');
                    var iframeWindow = them['layui-layer-iframe'+ index]
                    layero.find('iframe').contents().find('.toCheck').click(function(){
                        $.post("{:url('/erp.FinanceVoucher/check')}", {period_code:period_code}, function(res) {
                            iframeWindow.layer.msg(res.msg);
                            if (res.code == 1) {
                                iframeWindow.location.reload();
                                tableIns.reload();
                            }
                        });
                    })
                }
                ,yes: function(index, layero){
                    them.layer.closeAll();
                }
                ,cancel: function(){
                    them.layer.closeAll();
                }
            });
            return false;
        });
    });
</script>
{/block}